<?php

namespace apiend\models;

use Yii;
use yii\helpers\Inflector;

/**
 * 站点授权菜单
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class SiteAuthMenu extends \common\models\site\SiteAuthMenu {

    public function getMeta() {
        return [
            'title' => $this->title,
            'icon' => $this->icon,
            'keepAlive' => boolval($this->keep_alive),
        ];
    }

    public function getPathUrl() {
        return '/' . ltrim($this->path, '/');
    }

    public function getName() {
        $path = ltrim($this->path, '/');
        if ($this->type) {
            return $path;
        }
        return Inflector::id2camel($this->path, '/');
    }

    public function getRedirect() {
        $path = ltrim($this->path, '/');
        if ($this->type) {
            return $path;
        }
        return Inflector::id2camel($this->path, '/');
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        $fields = [
            'path' => 'pathUrl',
            'template' => function() {
                return '/' . ltrim($this->template, '/');
            },
            'name',
            'meta',
            'parent' => 'parent_id',
        ];
        if (!empty($this->children)) {
            $fields[] = 'children';
            $fields['redirect'] = function() {
                $first = current($this->children);
                return $first->pathUrl;
            };
        }
        if ($this->hidden) {
            $fields['hidden'] = function() {
                return boolval($this->hidden);
            };
        }
        return $fields;
    }

}
